package vn.com.ltv.lo.daos;

import java.util.ArrayList;

import vn.com.ltv.lo.dtos.RatingDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class RatingDAO {
	protected static final String TABLE_NAME = "Rating";
	protected static final String ID = "rowid";
	protected static final String RT_ID = "rt_id";
	protected static final String RT_PNT = "rt_pnt";
	protected static final String USR_ID = "usr_id";
	protected static final String MN_ID = "mn_id";
	protected static String TABLE_CREATION_STRING = "create table "
			+ TABLE_NAME + " (" + ID + " integer primary key autoincrement, "
			+ RT_ID + " text, " + RT_PNT + " integer, " + USR_ID + " text, "
			+ MN_ID + " text);";
	protected static String TABLE_DROP_STRING = "drop table if exists"
			+ TABLE_NAME + ";";
	private Context context;

	public RatingDAO(Context context) {
		this.context = context;
	}

	public ArrayList<RatingDTO> getAll() {
		ArrayList<RatingDTO> ratingDTOs = new ArrayList<RatingDTO>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, null, null,
				null, null, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				RatingDTO ratingDTO = this.cursorToRatingDTO(cursor);
				ratingDTOs.add(ratingDTO);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return ratingDTOs;
	}

	public RatingDTO get(int id) {
		RatingDTO ratingDTO = null;
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, ID + "=?",
				new String[] { String.valueOf(id) }, null, null, null);
		if (cursor.moveToFirst()) {
			ratingDTO = this.cursorToRatingDTO(cursor);
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return ratingDTO;
	}

	public void insert(RatingDTO ratingDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		ContentValues contentValues = this.ratingDTOToContentValues(ratingDTO);
		sQLiteDatabase.insert(TABLE_NAME, null, contentValues);
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	/** * @return the number of rows affected */
	public int update(RatingDTO ratingDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		int id = ratingDTO.getId();
		ratingDTO.setId(-1);
		ContentValues contentValues = this.ratingDTOToContentValues(ratingDTO);
		int rowCount = sQLiteDatabase.update(TABLE_NAME, contentValues, ID
				+ "=?", new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
		return rowCount;
	}

	public void delete(int id) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		sQLiteDatabase.delete(TABLE_NAME, ID + "=?",
				new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	private RatingDTO cursorToRatingDTO(Cursor cursor) {
		RatingDTO ratingDTO = new RatingDTO();
		int id = cursor.getInt(cursor.getColumnIndex(ID));
		ratingDTO.setId(id);
		String rtId = cursor.getString(cursor.getColumnIndex(RT_ID));
		ratingDTO.setRtId(rtId);
		int rtPnt = cursor.getInt(cursor.getColumnIndex(RT_PNT));
		ratingDTO.setRtPnt(rtPnt);
		String usrId = cursor.getString(cursor.getColumnIndex(USR_ID));
		ratingDTO.setUsrId(usrId);
		String mnId = cursor.getString(cursor.getColumnIndex(MN_ID));
		ratingDTO.setMnId(mnId);
		return ratingDTO;
	}

	private ContentValues ratingDTOToContentValues(RatingDTO ratingDTO) {
		ContentValues contentValues = new ContentValues();
		int id = ratingDTO.getId();
		if (id >= 0) {
			contentValues.put(ID, id);
		}
		String rtId = ratingDTO.getRtId();
		if (rtId != null) {
			contentValues.put(RT_ID, rtId);
		}
		int rtPnt = ratingDTO.getRtPnt();
		if (rtPnt >= 0) {
			contentValues.put(RT_PNT, rtPnt);
		}
		String usrId = ratingDTO.getUsrId();
		if (usrId != null) {
			contentValues.put(USR_ID, usrId);
		}
		String mnId = ratingDTO.getMnId();
		if (mnId != null) {
			contentValues.put(MN_ID, mnId);
		}
		return contentValues;
	}
}